package com.seaboxdata.threadHandler.yxzsxt;

import com.alibaba.fastjson.JSONObject;
import com.seaboxdata.entity.yxzsxt.IntersectionDelayDetails;
import com.seaboxdata.entity.yxzsxt.LinkDataForTgis;
import com.seaboxdata.kafka.BaseStringKafkaConsumer;
import com.seaboxdata.kafka.BaseStringKafkaProducer;
import com.seaboxdata.service.yxzsxt.IntersectionDelayDetailsService;
import com.seaboxdata.service.yxzsxt.LinkDataForTgisService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName IntersectionDelayDetailsHandler
 * @Deacription TODO 获取某一交叉口各个方向的延误信息(转向信息)
 * @Author dhjie
 * @Date 2021/8/26
 * @Version 1.0
 **/
@Component
@Slf4j
public class IntersectionDelayDetailsHandler implements DisposableBean, Runnable{

    private Thread thread;

    @Autowired
    private IntersectionDelayDetailsService intersectionDelayDetailsService;

    @Autowired
    private BaseStringKafkaConsumer baseStringKafkaConsumer;

    @Autowired
    private BaseStringKafkaProducer baseStringKafkaProducer;

    @Value("${real.trans.IntersectionDelayDetailsTopic}")
    private String topic;

    @Value("${interface.isTest}")
    private String isTest;

    private boolean flag;

    @Autowired
    public void initThread() {
        if("true".equals(isTest)){
            return;
        }
        this.flag = true;
        this.thread = new Thread(this);
        thread.setName("IntersectionDelayDetailsHandler");
        this.thread.start();
        log.info("IntersectionDelayDetailsHandler start");
    }

    @Override
    public void run() {
        KafkaConsumer<String, String> consumer = baseStringKafkaConsumer.getConsumer(topic);
        while (flag) {
            boolean success = true;
            ConsumerRecords<String, String> messages = consumer.poll(Duration.ofMillis(100L));
            List<IntersectionDelayDetails> results = new ArrayList<>();
            for (ConsumerRecord<String, String> message : messages) {
                results.add(JSONObject.parseObject(message.value(), IntersectionDelayDetails.class));
            }
            //写入数据库
            if (!CollectionUtils.isEmpty(results)) {
                try {
                    intersectionDelayDetailsService.saveBatch(results);
                    consumer.commitSync();
                } catch (Exception e) {
                    log.error("道路实时路况接口数据入库 异常:"+e.getMessage());
                    e.printStackTrace();
                    //success = false;
                }

                /*if (success) {
                    try {
                        roadOperationMonitoringService.saveBatch(results);
                    } catch (Exception e) {
                        log.error("batch insert roadOperationMonitoring failed");
                        e.printStackTrace();
                        for (RoadOperationMonitoring roadOperationMonitoring : results) {
                            try {
                                baseStringKafkaProducer.sendMessageToKafka(JSONObject.toJSONString(roadOperationMonitoring), topic);
                            } catch (Exception e1) {
                                log.error("write database failed and write kafka roadOperationMonitoring error");
                                e1.printStackTrace();
                            }
                        }
                    }
                }*/
            }
        }
    }

    @Override
    public void destroy() throws Exception {
        this.flag = false;
    }

}
